<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>算法管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchName" @input="inputAlter" placeholder="算法名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">
          搜索
        </el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleUpload()">
          上传算法包(更新)
        </el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="
        dataList.slice(
          (this.pageparm.currentPage - 1) * this.pageparm.pageSize,
          this.pageparm.currentPage * this.pageparm.pageSize
        )
      " highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column sortable prop="name" label="算法名称"> </el-table-column>
      <el-table-column sortable prop="exec" label="算法类型"> </el-table-column>
      <el-table-column sortable prop="args" label="参数说明"> </el-table-column>
      <el-table-column sortable prop="updated_at" label="创建时间">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" class="opt-btns">
            算法配置
          </el-button>
          <el-switch v-model="scope.row.version" active-text="启动" @change="switchAlgs(scope.$index, scope.row)">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <!-- 上传对话框 -->
    <el-dialog title="上传算法包(更新)" :visible.sync="uploadDialogVisible" width="30%" @close="closeUploadDialog">
      <div class="container">
        <el-upload class="upload" drag :action="upurl" :headers="headers" :before-upload="beforeUpload">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将算法可执行文件打包后(.zip)拖到此处，或<em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">
            只能上传算法包，zip包文件名预算法可执行文件同名，且zip包内不能嵌套文件夹。
          </div>
        </el-upload>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
  import Pagination from "../../components/Pagination";
  export default {
    data() {
      return {
        uploadDialogVisible: false,
        upurl: cfg.baseURL + "/algs/upload/",
        headers: {
          Authorization: "Bearer " + localStorage.getItem("token"),
        },
        algShow: true, //switch开启
        loading: false, //是显示加载
        title: "添加",
        dataList: [], //列表数据
        searchName: "", //搜索框内容
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10,
        },
        // 算法执行弹框显隐
        ifExecute: false,
        // 选取算法执行的id
        ExecuteID: "",
        // 输入的影像数据id
        imageIdValue: "",
      };
    },
    // 注册组件
    components: {
      Pagination,
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getDataList();
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取算法列表
      getDataList() {
        this.$axios.get("/algs/").then((res) => {
          this.dataList = res.data.results.list;
          this.pageparm.total = res.data.results.total;
        });
      },

      // 输入框内容改变
      inputAlter() {
        if (this.searchName == "") {
          this.getDataList();
        } else {
          this.dataList = this.dataList.filter((value) => {
            return value.name.match(this.searchName);
          });
          this.pageparm.total = this.dataList.length;
        }
      },
      // 搜索事件
      search() {
        this.dataList = this.dataList.filter((value) => {
          return value.name.match(this.searchName);
        });
        this.pageparm.total = this.dataList.length;
      },
      // 分页插件事件
      callFather(parm) {
        this.pageparm.currentPage = parm.currentPage;
        this.pageparm.pageSize = parm.pageSize;
      },
      //显示编辑界面
      handleEdit(index, row) {
        this.editFormVisible = true;
        this.$router.push({ path: "/AlgConfig", query: { row } });
      },
      // 删除
      switchAlgs(index, row) {
        let v = 1;
        if (row.version) {
          v = 0;
        }
        this.$axios
          .post("/algs/info/" + row.id + "/", {
            version: v,
          })
          .then((res) => {
            this.getDataList();
          });
      },
      handleUpload() {
        this.uploadDialogVisible = true;
      },
      closeUploadDialog() {
        this.uploadDialogVisible = false;
        this.getDataList();
      },
      beforeUpload(file) {
        let lext = file.name.substr(file.name.lastIndexOf(".")).toLowerCase();
        const iszip = lext === ".zip";
        if (!iszip) {
          this.$message.error("只能上传同名.zip包格式!");
        }

        let algs = ["slope", "aspect", "vfc", "ndwi", "ndsi", "lst", "tvdi", "water", "route", "station","shigong","zhanshu","tongxing"]
        const isInList = algs.indexOf(file.name.substring(0, file.name.length - 4));
        if (isInList == -1) {
          this.$message.error("未能识别的算法类型!");
        }

        return iszip && isInList != -1;
      },
    },
  };
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .opt-btns {
    margin-right: 10px;
  }

  .container {
    width: 100%;
    display: flex;
    justify-content: center;
  }

  .upload {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
</style>